#region Summary

// 
// Owner : JackieHan 
// 
// Solution : Jackie.Utility
// 
// Project :Jackie.Utility
// 
// File : JackieConverter.cs
// 
// CopyRight : Copyright © 2009-2010 JackieHan Personal, All Rights Reserved 
// 
// Summary : 自定义类型转换
// 
// History : 
//			2010-08-31 16:10:39	    Create 
// 

#endregion

#region Using

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Jackie.Utility.CustomExtension;

#endregion

namespace Jackie.Utility
{
    /// <summary> 
    /// 自定义类型转换
    /// </summary> 
    public class JackieConvertor
    {
        public static FileType ToFileType(string extension)
        {
            if (extension == ".xml")
                return FileType.Xml;
            if (extension == ".txt")
                return FileType.Text;
            if (extension == ".xls")
                return FileType.Excel;
            if (extension == ".csv")
                return FileType.Csv;
            return FileType.None;
        }

        /// <summary>
        /// 将时间转换为Unix时间戳
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static double ToUnixTime(DateTime dateTime)
        {
            var startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            return (dateTime - startTime).TotalSeconds;
        }

        /// <summary>
        /// 将Unix时间戳转换成时间
        /// </summary>
        /// <param name="unixTicks"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(double unixTicks)
        {
            var startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            return startTime.AddSeconds(unixTicks);
        }

        #region To Decimal

        /// <summary>
        /// 转换成Decimal,转换失败则设置成初始值
        /// </summary>
        /// <param name="strValue">字符串</param>
        /// <returns></returns>
        public static decimal ToDecimal(string strValue)
        {
            return ToDecimal(strValue, false);
        }

        /// <summary>
        /// 转换成Decimal
        /// </summary>
        /// <exception cref="FormatException">当字符串格式不正确，抛出此异常</exception>
        /// <param name="strValue">字符串</param>
        /// <returns></returns>
        public static decimal ToDecimalThrowException(string strValue)
        {
            return ToDecimal(strValue, true);
        }

        /// <summary>
        /// 转换成Decimal
        /// </summary>
        /// <exception cref="FormatException">当字符串格式不正确，抛出此异常</exception>
        /// <param name="strValue">字符串</param>
        /// <param name="isThrowException">是否抛出异常</param>
        /// <returns></returns>
        protected static decimal ToDecimal(string strValue, bool isThrowException)
        {
            decimal decValue = decimal.Zero;
            if (!string.IsNullOrEmpty(strValue))
            {
                bool success = decimal.TryParse(strValue, out decValue);
                if (isThrowException && !success)
                    throw new FormatException(String.Format("{0} convert to decimal error", strValue));
            }
            return decValue;
        }

        #endregion

        #region ToDateTime

        /// <summary>
        /// 将字符串转换成时间类型，要是转换失败则设置为最小时间
        /// </summary>
        /// <param name="strValue">字符串</param>
        /// <returns></returns>
        public static DateTime ToDateTime(string strValue)
        {
            return ToDateTime(strValue, false);
        }

        /// <summary>
        /// 将字符串转换成时间类型，转换失败掏出异常
        /// </summary>
        /// <exception cref="FormatException">当字符串格式不正确，抛出此异常</exception>
        /// <param name="strValue">字符串</param>
        /// <returns></returns>
        public static DateTime ToDataTimeThrowException(string strValue)
        {
            return ToDateTime(strValue, true);
        }

        /// <summary>
        /// 转换成时间格式DateTime
        /// </summary>
        /// <exception cref="FormatException">当字符串格式不正确，抛出此异常</exception>
        /// <param name="strValue">字符串</param>
        /// <param name="isThrowException">是否抛出异常</param>
        /// <returns></returns>
        protected static DateTime ToDateTime(string strValue, bool isThrowException)
        {
            DateTime dateValue = DateTimeExtension.MinDate;
            if (!string.IsNullOrEmpty(strValue))
            {
                if (IsNumber(strValue))
                {
                    dateValue = ConvertIntDateTime(ToDouble(strValue));
                    return dateValue;
                }
                bool success = DateTime.TryParse(strValue, out dateValue);
                if (isThrowException && !success)
                    throw new FormatException(string.Format("{0} convert to datetime error!", strValue));
            }
            return dateValue;
        }

        /// <summary>
        /// 时间戳转换(MySql的时间戳转换成正常时间)
        /// </summary>
        /// <param name="timeNumber">时间偏移量</param>
        /// <returns></returns>
        private static DateTime ConvertIntDateTime(double timeNumber)
        {
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            DateTime time = startTime.AddSeconds(timeNumber);
            return time;
        }

        private static bool IsNumber(string strValue)
        {
            Regex numberReg = new Regex(@"^\d+$");
            return numberReg.IsMatch(strValue);
        }


        #endregion

        #region ToInt

        /// <summary>
        /// 将字符串转换成Int型，转换失败是设置为最小值
        /// </summary>
        /// <param name="strValue">字符串</param>
        /// <returns></returns>
        public static int ToInt(string strValue)
        {
            return ToInt(strValue, false);
        }

        /// <summary>
        /// 转换成Int类型
        /// </summary>
        /// <exception cref="FormatException">当字符串格式不正确，抛出此异常</exception>
        /// <param name="strValue">字符串</param>
        /// <returns></returns>
        public static int ToIntThrowException(string strValue)
        {
            return ToInt(strValue, true);
        }

        /// <summary>
        /// 转换成Int类型
        /// </summary>
        /// <exception cref="FormatException">当字符串格式不正确，抛出此异常</exception>
        /// <param name="strValue">字符串</param>
        /// <param name="isThrowException">是否抛出异常</param>
        /// <returns></returns>
        protected static int ToInt(string strValue, bool isThrowException)
        {
            int intValue = 0;
            if (!string.IsNullOrEmpty(strValue))
            {
                bool success = int.TryParse(strValue, out intValue);
                if (isThrowException && !success)
                    throw new FormatException(string.Format("{0} convert to int error!", strValue));
            }
            return intValue;
        }

        #endregion

        #region ToFloat

        /// <summary>
        /// 将字符串转换成Float型，转换失败是设置为最小值
        /// </summary>
        /// <param name="strValue">字符串</param>
        /// <returns></returns>
        public static float ToFloat(string strValue)
        {
            return ToFloat(strValue, false);
        }

        /// <summary>
        /// 转换成Float类型
        /// </summary>
        /// <exception cref="FormatException">当字符串格式不正确，抛出此异常</exception>
        /// <param name="strValue">字符串</param>
        /// <returns></returns>
        public static float ToFloatThrowException(string strValue)
        {
            return ToFloat(strValue, true);
        }

        /// <summary>
        /// 转换成Float类型
        /// </summary>
        /// <exception cref="FormatException">当字符串格式不正确，抛出此异常</exception>
        /// <param name="strValue">字符串</param>
        /// <param name="isThrowException">是否抛出异常</param>
        /// <returns></returns>
        protected static float ToFloat(string strValue, bool isThrowException)
        {
            float floatValue = 0f;
            if (!string.IsNullOrEmpty(strValue))
            {
                bool success = float.TryParse(strValue, out floatValue);
                if (isThrowException && !success)
                    throw new FormatException(string.Format("{0} convert to int error!", strValue));
            }
            return floatValue;
        }

        #endregion

        #region ToDouble

        /// <summary>
        /// 将字符串转换成Double型，转换失败是设置为最小值
        /// </summary>
        /// <param name="strValue">字符串</param>
        /// <returns></returns>
        public static double ToDouble(string strValue)
        {
            return ToDouble(strValue, false);
        }

        /// <summary>
        /// 转换成Double类型
        /// </summary>
        /// <exception cref="FormatException">当字符串格式不正确，抛出此异常</exception>
        /// <param name="strValue">字符串</param>
        /// <returns></returns>
        public static double ToDoubleThrowException(string strValue)
        {
            return ToDouble(strValue, true);
        }

        /// <summary>
        /// 转换成Double类型
        /// </summary>
        /// <exception cref="FormatException">当字符串格式不正确，抛出此异常</exception>
        /// <param name="strValue">字符串</param>
        /// <param name="isThrowException">是否抛出异常</param>
        /// <returns></returns>
        protected static double ToDouble(string strValue, bool isThrowException)
        {
            double doubleValue = 0;
            if (!string.IsNullOrEmpty(strValue))
            {
                bool success = double.TryParse(strValue, out doubleValue);
                if (isThrowException && !success)
                    throw new FormatException(string.Format("{0} convert to int error!", strValue));
            }
            return doubleValue;
        }

        #endregion

        #region ToLong

        /// <summary>
        /// 将字符串转换成Long型，转换失败是设置为最小值
        /// </summary>
        /// <param name="strValue">字符串</param>
        /// <returns></returns>
        public static long ToLong(string strValue)
        {
            return ToLong(strValue, false);
        }

        /// <summary>
        /// 转换成Long类型
        /// </summary>
        /// <exception cref="FormatException">当字符串格式不正确，抛出此异常</exception>
        /// <param name="strValue">字符串</param>
        /// <returns></returns>
        public static long ToLongThrowException(string strValue)
        {
            return ToLong(strValue, true);
        }

        /// <summary>
        /// 转换成Long类型
        /// </summary>
        /// <exception cref="FormatException">当字符串格式不正确，抛出此异常</exception>
        /// <param name="strValue">字符串</param>
        /// <param name="isThrowException">是否抛出异常</param>
        /// <returns></returns>
        protected static long ToLong(string strValue, bool isThrowException)
        {
            long doubleValue = 0;
            if (!string.IsNullOrEmpty(strValue))
            {
                bool success = long.TryParse(strValue, out doubleValue);
                if (isThrowException && !success)
                    throw new FormatException(string.Format("{0} convert to long error!", strValue));
            }
            return doubleValue;
        }

        #endregion

        #region To Boolean

        /// <summary>
        /// 将字符串转换成Boolean型，转换失败是设置为最小值
        /// </summary>
        /// <param name="strValue">字符串</param>
        /// <returns></returns>
        public static bool ToBoolean(string strValue)
        {
            return ToBoolean(strValue, false);
        }

        /// <summary>
        /// 转换成Boolean类型
        /// </summary>
        /// <exception cref="FormatException">当字符串格式不正确，抛出此异常</exception>
        /// <param name="strValue">字符串</param>
        /// <returns></returns>
        public static bool ToBooleanThrowException(string strValue)
        {
            return ToBoolean(strValue, true);
        }

        /// <summary>
        /// 转换成Boolean类型
        /// </summary>
        /// <exception cref="FormatException">当字符串格式不正确，抛出此异常</exception>
        /// <param name="strValue">字符串</param>
        /// <param name="isThrowException">是否抛出异常</param>
        /// <returns></returns>
        protected static bool ToBoolean(string strValue, bool isThrowException)
        {
            bool boolValue = false;
            if (!string.IsNullOrEmpty(strValue))
            {
                bool success = bool.TryParse(strValue, out boolValue);
                if (isThrowException && !success)
                    throw new FormatException(string.Format("{0} convert to int error!", strValue));
            }
            return boolValue;
        }

        #endregion

        #region To String   转换成固定长度的字符串  不足补0

        public static string ToString(long num, int length)
        {
            var numStr = num.ToString();
            var zeroLenght = length - numStr.Length;
            var zeroStr = string.Empty;
            for (var i = 0; i < zeroLenght; i++)
            {
                zeroStr += "0";
            }
            return zeroStr + numStr;
        }

        #endregion
    }
}